#!/usr/bin/env python
# -*- coding:UTF-8 -*-
from django.shortcuts import render
from django.http import JsonResponse
#from django.db import
from models import JdSystem
from models import JdDeviceCol
from models import JdDeviceLoc
from models import JdDeviceDet
from models import JdCheck
from models import JdInspection
from models import JdDailyInspection
from models import JdDailyFault
from models import JdDailyMaintain,JdDaiMaiPhoto
from models import JdOftenInspection
from models import JdOftenFault, JdOftFauPhoto
from models import DailyFaultHistoryList
from models import DailyFaultHistoryPhoto, JdDailyFaultPhoto
from models import OftenFaultHistoryList, OftenFaultHistoryPhoto
from django.http import HttpResponse, HttpResponseRedirect, StreamingHttpResponse, JsonResponse
from django.shortcuts import render, render_to_response
import web_XZDQ_V1_0.MySQLDB as MySQLDB
import web_XZDQ_V1_0.PowerID as PowerID
import web_XZDQ_V1_0.Common as Common
import web_XZDQ_V1_0.FileUtil as FileUtil
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
import time
import json
from django.contrib.auth.models import User
from models import Person,Company


# Create your views here.


# 装饰器html 权限验证 系统管理id=21
def auth(func):
    def inner(request):
        # 权限验证
        menudate = []
        try:
            userid = request.COOKIES['userid']
            row0 = MySQLDB.SelectMySql("SELECT role_id FROM t_sys_user WHERE user_id = %s AND status = 1;" % userid)
            # if row0[0][1] == 0:
            #    response = render(request,'login.html', {})
            #    return response
            row1 = MySQLDB.SelectMySql("SELECT menu_id FROM t_role_menu WHERE role_id = %s;" % row0[0][0])
            for i in range(len(row1)):
                menudate.append(row1[i][0])
        except:
            pass
        if 21 not in menudate:
            # return HttpResponseRedirect('/')
            response = render(request, 'login.html', {})
            return response
        # 多用户登录验证
        md5str = request.COOKIES['loginmd5']
        row0 = MySQLDB.SelectMySql("SELECT user_id FROM t_sys_user WHERE valid_md5 = '%s';" % md5str)
        try:
            if str(row0[0][0]) == userid:
                pass
            else:
                # return HttpResponseRedirect('/')
                response = render(request, 'login.html', {})
                return response
        except:
            # return HttpResponseRedirect('/')
            response = render(request, 'login.html', {})
            return response
        return func(request)

    return inner


# 装饰器json 权限验证 系统管理id=21
def authjson(func):
    def jnner(request):
        # 权限验证
        menudate = []
        try:
            userid = request.COOKIES['userid']
            row0 = MySQLDB.SelectMySql("SELECT role_id FROM t_sys_user WHERE user_id = %s AND status = 1;" % userid)
            row1 = MySQLDB.SelectMySql("SELECT menu_id FROM t_role_menu WHERE role_id = %s;" % row0[0][0])
            for i in range(len(row1)):
                menudate.append(row1[i][0])
        except:
            data = {
                "status": 0,
                "message": u"登录超时，请重新登陆!",
                "data": []
            }
            jsonstr = json.dumps(data)
            return HttpResponse(jsonstr, content_type="application/json")
        if 21 not in menudate:
            data = {
                "status": 0,
                "message": u"权限不够！",
                "data": []
            }
            jsonstr = json.dumps(data)
            return HttpResponse(jsonstr, content_type="application/json")
        # 多用户登录验证
        md5str = request.COOKIES['loginmd5']
        row0 = MySQLDB.SelectMySql("SELECT user_id FROM t_sys_user WHERE valid_md5 = '%s';" % md5str)
        try:
            if str(row0[0][0]) == userid:
                pass
            else:
                data = {
                    "status": 0,
                    "message": u"该账号在其他地方登录",
                    "data": []
                }
                jsonstr = json.dumps(data)
                return HttpResponse(jsonstr, content_type="application/json")
        except:
            data = {
                "status": 0,
                "message": u"其他错误",
                "data": []
            }
            jsonstr = json.dumps(data)
            return HttpResponse(jsonstr, content_type="application/json")
        return func(request)

    return jnner



# 功能：机电巡检基础数据页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_index(request):
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_index.html", {})

# 功能：机电巡检基础数据系统参数页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_XTCS(request):
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_XTCS.html", {})

# 功能：机电巡检基础数据系统参数页面数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_XTCS_DATA(request):
    SysInfo = []
    rows = JdSystem.objects.all()                        # 获取数据库数据
    for row in rows:                                        # 数据打包
        info = {}
        info["id"] = row.id
        info["sysname"] = row.systemName
        info["sysenable"] = row.systemEnable
        SysInfo.append(info)                                # 数据打包结束
    data = {                                                # 按前端的通信规则进行格式封装
        "status": 1,
        "message": u"查询系统参数数据成功",
        "data": SysInfo
    }                                                       # 封装结束
    return JsonResponse(data)                               # 返回Json格式

# 功能：机电巡检基础数据系统参数新增页面保存数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_XTCS_ADD_SAVE(request):
    if request.POST:
        systemAdd = JdSystem.objects.create(systemName=request.POST['sysname'],
                                            systemEnable=request.POST['sysstatus'])
        systemAdd.save()
        data = {
            "status": 1,
            "message": u"系统添加成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"系统添加失败1",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据系统参数新增页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_XTCS_ADD(request):
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_XTCS_ADD.html")

# 功能：机电巡检基础数据系统参数修改页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_XTCS_MOD(request):
    rows = JdSystem.objects.get(id=request.GET['section_id'])
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_XTCS_MODIFY.html", {'sysinfo': rows})

# 功能：机电巡检基础数据系统参数修改页面保存数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_XTCS_MODSAVE(request):
    if request.POST:
        rows = JdSystem.objects.get(id=request.POST['section_id'])
        rows.systemName = request.POST['sysname']
        rows.systemEnable = request.POST['sysstatus']
        rows.save()
        data = {
            "status": 1,
            "message": u"系统参数修改成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        pass


# 功能：机电巡检基础数据系统参数删除页面数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_XTCS_DEL(request):
    if request.POST:
        rows = JdSystem.objects.get(id=request.POST['section_id'])
        obj = JdDeviceCol.objects.filter(systemName=rows.id)
        if not obj.exists():
            JdSystem.objects.filter(id=rows.id).delete()
            data = {
                "status": 1,
                "message": u"系统参数删除成功",
                "data": []
                }
            return JsonResponse(data)
        else:
            data = {
                "status": 0,
                "message": u"请先删除系统下端设备",
                "data": []
            }
        return JsonResponse(data)
    else:
        pass

# 功能：机电巡检基础数据设备类别页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBLB(request):
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_SBLB.html", {})

# 功能：机电巡检基础数据设备类别新增页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBLB_ADD(request):
    rows = JdSystem.objects.all()
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_SBLB_ADD.html", {'rows': rows})

# 功能：机电巡检基础数据设备类别页面数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBLB_DATA(request):
    DevciceInfo = []
    rows = JdDeviceCol.objects.all()                        # 获取数据库数据
    for row in rows:                                        # 数据打包
        info = {}
        info["id"] = row.id
        info["deviceName"] = row.deviceName
        info["systemName"] = row.systemName.systemName
        info['status'] = row.status
        DevciceInfo.append(info)
    data = {
        "status": 1,
        "message": u"查询系统参数数据成功",
        "data": DevciceInfo
    }
    return JsonResponse(data)

# 功能：机电巡检基础数据设备类别新增页面保存数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBLB_ADDSAVE(request):
    if request.POST:
        deviceAdd = JdDeviceCol.objects.create(deviceName=request.POST['devicename'],
                                               status=request.POST['devicestatus'],
                                               systemName=JdSystem.objects.get(id=request.POST['sysname']))
        deviceAdd.save()
        data = {
            "status": 1,
            "message": u"设备类别添加成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"设备类别添加失败",
            "data": []
        }
    return JsonResponse(data)

# 功能：机电巡检基础数据设备类别修改页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBLB_MOD(request):
    rows = JdSystem.objects.all()
    obj = JdDeviceCol.objects.get(id=request.GET['section_id'])
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_SBLB_MODIFY.html", {'obj': obj, 'rows': rows})

# 功能：机电巡检基础数据设备类别修改页面保存数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBLB_MODSAVE(request):
    try:
        rows = JdDeviceCol.objects.get(id=request.POST['deviceid'])
        rows.systemName = JdSystem.objects.get(id=request.POST['sysname'])
        rows.deviceName = request.POST['devicename']
        rows.status = request.POST['devicestatus']
        rows.save()
        data = {
            "status": 1,
            "message": u"设备类别修改成功",
            "data": []
        }
        return JsonResponse(data)
    except:
        data = {
                "status": 0,
                "message": u"设备类别修改失败",
                 "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据设备类别删除页面数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBLB_DEL(request):
    try:
        rows = JdDeviceCol.objects.get(id=request.POST['section_id'])
        JdDeviceCol.objects.filter(id=rows.id).delete()
        data = {
             "status": 1,
             "message": u"设备类别删除成功",
             "data": []
         }
        return JsonResponse(data)
    except:
        data = {
            "status": 0,
            "message": u"设备类别删除失败",
             "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据设备位置页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBWZ(request):
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_SBWZ.html", {})

# 功能：机电巡检基础数据设备位置页面数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBWZ_DATA(request):
    try:
        SysDeviceLoc = []
        rows = JdDeviceLoc.objects.all()                        # 获取数据库数据
        for row in rows:                                        # 数据打包
            info = {}
            info["id"] = row.id
            info["locationname"] = row.deviceLocation
            info["status"] = row.status
            SysDeviceLoc.append(info)                                # 数据打包结束
        data = {                                                # 按前端的通信规则进行格式封装
            "status": 1,
            "message": u"查询系统参数设备位置成功",
            "data": SysDeviceLoc
        }                                                       # 封装结束
        return JsonResponse(data)                               # 返回Json格式
    except:
        data = {
            "status": 0,
            "message": u"查询系统参数设备位置失败",
            "data": []
        }
        return JsonResponse(data)


    # 功能：机电巡检基础数据设备类别新增页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBWZ_ADD(request):
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_SBWZ_ADD.html", {})

# 功能：机电巡检基础数据系统参数新增页面保存数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBWZ_ADDSAVE(request):
    try:
        deviceLocAdd = JdDeviceLoc.objects.create(deviceLocation=request.POST['locationname'],
                                                  status=request.POST['status'])
        deviceLocAdd.save()
        data = {
            "status": 1,
            "message": u"设备位置添加成功",
            "data": []
        }
        return JsonResponse(data)
    except:
        data = {
            "status": 0,
            "message": u"设备位置添加失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据设备位置修改页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBWZ_MOD(request):
    rows = JdDeviceLoc.objects.get(id=request.GET['location_id'])
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_SBWZ_MODIFY.html", {'sysinfo': rows})

# 功能：机电巡检基础数据系统参数修改页面保存数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBWZ_MODSAVE(request):
    try:
        rows = JdDeviceLoc.objects.get(id=request.POST['loction_id'])
        rows.deviceLocation = request.POST['loctionname']
        rows.status = request.POST['status']
        rows.save()
        data = {
            "status": 1,
            "message": u"设备位置修改成功",
            "data": []
        }
        return JsonResponse(data)
    except:
        data = {
                "status": 0,
                "message": u"设备位置修改失败",
                 "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据设备位置删除页面数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBWZ_DEL(request):
    try:
        JdDeviceLoc.objects.get(id=request.POST['loction_id']).delete()
        data = {
             "status": 1,
             "message": u"设备位置删除成功",
             "data": []
         }
        return JsonResponse(data)
    except:
        data = {
            "status": 0,
            "message": u"设备位置删除失败",
             "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据设备详情页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBXQ(request):
    ob = JdDeviceCol.objects.all()
    qb = JdSystem.objects.all()
    return render(request, 'JDXJ/JCSJ/JDXJ_JCSJ_SBXQ.html', {'objs': ob, 'rows': qb})

# 功能：机电巡检基础数据设备详情查看页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBXQ_SEE(request):
    if request.GET:
        return render(request, 'JDXJ/JCSJ/JDXJ_JCSJ_SBXQ_SEE.html', {'systemid': request.GET['systemid'],
                                                                     'devienameid': request.GET['devienameid']})

# 功能：机电巡检基础数据设备详情查看页面数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBXQ_SEE_DATA(request):
    DeviceDet = []
    try:
        sys = JdSystem.objects.get(id=request.GET['systemid'])
        dec = JdDeviceCol.objects.get(id=request.GET['devienameid'])
        abs = JdDeviceDet.objects.filter(jdSystemName=sys.id,
                                         jdDeviceCategory=dec.id)
        for ab in abs:
            info = {}
            info["deviveid"] = ab.id
            info["sysname"] = ab.jdSystemName.systemName
            info["devicename"] = ab.jdDeviceCategory.deviceName
            info["location"] = ab.jdDeviceLocation.deviceLocation
            info["deviceno"] = ab.jdDeviceNumber
            info["device"] = ab.jdDeviceName
            DeviceDet.append(info)                                  # 数据打包结束
        data = {                                                    # 按前端的通信规则进行格式封装
            "status": 1,
            "message": u"查询设备详情数据成功",
            "data": DeviceDet
        }
        return JsonResponse(data)
    except:
        data = {
            "status": 0,
            "message": u"查询设备详情数据失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据设备详情新增设备页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBXQ_DEVICE_ADD(request):
    if request.GET:
        rows = JdDeviceLoc.objects.all()
        result = JdDeviceCol.objects.get(id=request.GET['devienameid'])
    return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_SBXQ_DEVICE_ADD.html", {'LocationInfo': rows,
                                                                        'devicename': result.deviceName,
                                                                        'sysname': result.systemName.systemName,
                                                                        'systemid': result.systemName.id,
                                                                        'devienameid': result.id})

# 功能：机电巡检基础数据设备详情新增设备保存页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBXQ_DEVICE_ADDSAVE(request):
    try:
        if request.POST:
            a = request.POST['parameter1']
            b = request.POST['parameter2']
            total= JdDeviceDet.objects.filter(jdDeviceCategory=request.POST['devienameid']).count()
            while int(a)<= int(b):
                c = str(a) + '#'
                d = JdDeviceCol.objects.get(id=request.POST['devienameid']).deviceName
                e = JdDeviceLoc.objects.get(id=request.POST['locationid']).deviceLocation
                f = str(e)+'-'+str(c)+str(d)
                deviceadd = JdDeviceDet.objects.create(jdDeviceNumber=c, jdDeviceName=f,
                                                       jdSystemName=JdSystem.objects.get(id=request.POST['systemid']),
                                                       jdDeviceCategory=JdDeviceCol.objects.get(id=request.POST['devienameid']),
                                                       jdDeviceLocation=JdDeviceLoc.objects.get(id=request.POST['locationid']))
                deviceadd.save()
                a = int(a) + 1
            data = {
                "status": 1,
                "message": u"设备新增成功",
                "data": []
            }
            return JsonResponse(data)
    except:
        data = {
            "status": 0,
            "message": u"设备新增失败",
             "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据设备详情删除页面数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_SBXQ_DEVICE_DEL(request):
    try:
        if request.GET:
            deletedata = request.GET['deviveid']
            ab = deletedata.split('|', )
            data = deletedata.count('|')+1
            a = 0
            while a<data:
                JdDeviceDet.objects.get(id=ab[a]).delete()
                a = int(a)+1
            data = {
                 "status": 1,
                 "message": u"设备删除成功",
                 "data": []
             }
            return JsonResponse(data)
    except:
        data = {
            "status": 0,
            "message": u"设备删除失败",
             "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据日巡内容页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR(request):
    if request.GET:
        row = request.GET['tp']
        return render(request, "JDXJ/JCSJ/JDXJ_JCSJ_JCNR.html", {'tp':row})


# 功能：机电巡检基础数据日巡页面数据
# 传入参数：request
# 返回参数：responese
def JDXJ_JCSJ_JCNR_DATA(request):
    try:
        if request.GET:
            row = request.GET['tp']
            devicecat=[]
            ob = JdCheck.objects.filter(jdCheckType=row)
            for obb in ob:
                info={}
                info['parent_name'] = obb.jdCheckSysNAME.systemName
                info['equipment_name'] = obb.jdCheckDevCat.deviceName
                info['id'] = obb.id
                devicecat.append(info)
            data = {
                "status": 1,
                "message": u"查询日巡数据成功",
                "data": devicecat
            }
            return JsonResponse(data)
    except:
        data = {
            "status": 0,
            "message": u"查询日巡数据失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据日巡页面获取检查项数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR_XM_DATA(request):
    try:
        if request.GET:
            c = JdCheck.objects.get(id=request.GET['id'])
            obbs = JdInspection.objects.filter(jdInspectionProjectId=c.id)
            devicedata = []
            for obb in obbs:
                info = {}
                info['check_item'] = obb.jdInspectionProject
                info['check_content'] = obb.jdInspectionContent
                info['status'] = obb.jdInspectionStatus
                info['check_id'] = obb.id
                devicedata.append(info)
            data = {
                "status": 1,
                "message": u"查询设备检查项数据成功",
                "data": devicedata
            }
            return JsonResponse(data)
    except:
        data = {
            "status": 0,
            "message": u"查询设备检查项数据失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据日巡页面新增设备页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR_DEVICE_ADD(request):
    if request.GET:
        tp = request.GET['tp']
        ab = JdSystem.objects.all()
        bc = JdDeviceCol.objects.all()
        return render(request,'JDXJ/JCSJ/JDXJ_JCSJ_JCNR_ADD.html', {'tp': tp, 'system': ab, 'equipment': bc})

# 功能：机电巡检基础数据日巡页面新增设备系统改变页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR_DEVICE_change(request):
    try:
        if request.GET:
            AbbInfo=[]
            abs = JdDeviceCol.objects.filter(systemName=int(request.GET['system_id']))
            for ab in abs:
                info={}
                info['id'] = ab.id
                info['name'] = ab.deviceName
                AbbInfo.append(info)
            data = {
                "status": 1,
                "message": u"新增设备系统改变页面成功",
                "data": AbbInfo
            }
            return JsonResponse(data)
    except:
        data = {
            "status": 1,
            "message": u"新增设备系统改变页面失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据日巡页面新增设备保存页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR_DEVICE_ADDSAVE(request):
    if request.POST:
        a = request.POST['system_id']
        b = request.POST['equipment_id']
        c = JdCheck.objects.filter(jdCheckSysNAME=JdSystem.objects.get(id=int(a)),
                                   jdCheckDevCat=JdDeviceCol.objects.get(id=int(b)),
                                   jdCheckType=request.POST['tp'])
        if not c.exists():
            abb = JdCheck.objects.create(jdCheckSysNAME=JdSystem.objects.get(id=int(a)),
                                         jdCheckDevCat=JdDeviceCol.objects.get(id=int(b)),
                                         jdCheckStatue=request.POST['sta'],
                                         jdCheckType=request.POST['tp'])
            abb.save()
            data = {
                "status": 1,
                "message": u"保存数据成功",
                "data": []
            }
            return JsonResponse(data)
        else:
            data = {
                "status": 0,
                "message": u"请勿重复生成设备",
                "data": []
            }
    else:
        data = {
            "status": 0,
            "message": u"保存数据失败",
             "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据日巡页面修改页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR_DEVICE_DELECT(request):
    if request.POST:
        a = request.POST['id']
        JdCheck.objects.get(id=int(a)).delete()
        data = {
            "status": 1,
            "message": u"删除设备成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"删除设备失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据日巡页面新增检查项页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR_DEVICE_CHECK_ADD(request):
    if request.GET:
        tpp = request.GET['tp']
        idd = int(request.GET['id'])
        abs = JdCheck.objects.get(jdCheckType=tpp, id=idd)
        c = abs.jdCheckSysNAME
        d = abs.jdCheckDevCat
        return render(request,'JDXJ/JCSJ/JDXJ_JCSJ_JCNR_CHECK_ADD.html',{'tp': tpp,
                                                                         'id': idd,
                                                                          'system': c,
                                                                          'equipment': d})

# 功能：机电巡检基础数据日巡页面新增检查项页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR_DEVICE_CHECK_ADDSAVE(request):
    if request.POST:
        a = request.POST['system_id']
        b = request.POST['equipment_id']
        c = JdCheck.objects.get(jdCheckDevCat=JdDeviceCol.objects.get(id=int(b)),
                                jdCheckType=request.POST['tp'])
        JdInspection.objects.create(jdInspectionProjectId=c,
                                    jdInspectionProject=request.POST['sys_remark'],
                                    jdInspectionContent=request.POST['equ_remark'],
                                    jdInspectionStatus=request.POST['sta'])
        data = {
            "status": 1,
            "message": u"保存检查项成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"保存检查项失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据日巡页面检查项修改页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR_DEVICE_CHECK_MODIFY(request):
    if request.GET:
        bb = JdInspection.objects.get(id=request.GET['check_id'])
        a = JdCheck.objects.get(id=bb.jdInspectionProjectId.id)
        name = a.jdCheckSysNAME
        device = a.jdCheckDevCat
        rows = JdInspection.objects.get(id=request.GET['check_id'])
        return render(request, 'JDXJ/JCSJ/JDXJ_JCSJ_JCNR_CHECK_MODIFY.html', {'name': name, 'device':device, 'listl': rows})

# 功能：机电巡检基础数据日巡页面检查项修改保存页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR_DEVICE_CHECK_MODIFYSAVE(request):
    if request.POST:
        rows = JdInspection.objects.get(id=request.POST['check_id'])
        rows.jdInspectionProject = request.POST['sys_remark']
        rows.jdInspectionContent = request.POST['equ_remark']
        rows.jdInspectionStatus = request.POST['sta']
        rows.save()
        data = {
            "status": 1,
            "message": u"检查项目项修改成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"检查项目项修改失败1",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检基础数据日巡页面检查项删除页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JCSJ_JCNR_DEVICE_CHECK_DELECT(request):
    if request.POST:
        JdInspection.objects.get(id=request.POST['check_id']).delete()
        data = {
            "status": 1,
            "message": u"检查项目项删除成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"检查项目项删除失败1",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电巡检日常巡检页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_index(request):
    return render(request, 'JDXJ/RCXC/JDXJ_RCXC_index.html')

# 功能：机电巡检日常巡检纪录页面
# 传入参数：request
# 返回参数：responese
@login_required()
def  JDXJ_RCXC_XCJL(request):
    return render(request, 'JDXJ/RCXC/JDXJ_RCXC_XCJL.html')

# 功能：机电巡检日常巡检维护记录页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_WHJL(request):
    return render(request, 'JDXJ/RCXC/JDXJ_RCXC_WHJL.html')

# 功能：机电巡检日常巡检记录新增页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_ADD(request):
    bbs = Company.objects.all()
    deptInfo = []
    b = []
    for bb in bbs:
        info ={}
        info['organize_id'] = bb.corganize_id
        info['name'] = bb.cname
        if bb.cname not in b:
            b.append(bb.cname)
            deptInfo.append(info)
    aas = Person.objects.all()
    userInfo = []
    for aa in aas:
        info ={}
        info['user_id'] = aa.puserid
        info['real_name'] = aa.puser.last_name
        userInfo.append(info)
    loginInfo =[]
    dd = User.objects.get(last_name=request.user.last_name)
    cc = Person.objects.get(puser_id=dd.id)
    info['user_id'] = cc.puserid
    info['real_name'] = cc.puser.last_name
    info['organize_id'] = cc.pdept_id
    loginInfo.append(info)
    return render(request, 'JDXJ/RCXC/JDXJ_RCXC_XCJL_ADD.html',{'userInfo':userInfo,'deptInfo':deptInfo,'loginInfo':loginInfo})

# 功能：新增巡查纪录页面选择公司之后加载用户信息
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_USER(request):
    if request.GET:
        aas = Person.objects.filter(pdept_id=request.GET['organize_id'])
        userInfo = []
        if len(aas)>0:
            for aa in aas:
                info = {}
                info['user_id'] = aa.puserid
                info['real_name'] = aa.puser.last_name
                userInfo.append(info)
        else:
            data = {
                "status": 0,
                "message": u"请在该部门下添加人员",
                "data": []
            }
            return JsonResponse(data)
        data = {
            "status": 1,
            "message": u"查询数据成功",
            "data": userInfo
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"查询数据失败",
            "data": []
        }
        return JsonResponse(data)


# 功能：机电日巡巡查纪录页面新增页面保存
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_ADDSAVE(request):
    if request.POST:
        JdDailyInspection.objects.create(jdDaiInsTime=request.POST['checkdate'],
                                         jdDaiInsWeather=request.POST['weather'],
                                         jdDaiInsTemperature=request.POST['temperature'],
                                         jdDaiInsFaultCompanyid=request.POST['organize_id'],
                                         jdDaiInsFaultCompany=request.POST['name'],
                                         jdDaiInsFaultPeopleid=request.POST['user_id'],
                                         jdDaiInsFaultPeople=request.POST['real_name'])

        company =[]
        info = {}
        info['organize_id']= request.POST['organize_id']
        info['name'] = request.POST['name']
        info['user_id'] = request.POST['user_id']
        info['real_name'] = request.POST['real_name']
        company.append(info)
        data = {
            "status": 1,
            "message": u"检查记录新增成功",
            "data": company
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"检查记录新增失败",
            "data":[]
        }
        return JsonResponse(data)

# 功能：机电日巡巡查纪录页面数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_DATA(request):
    if request.GET:
        rows = JdDailyInspection.objects.all()
        dailyInfo=[]
        for row in rows:
            info ={}
            info['check_id'] = row.id
            row.jdDaiInsFaultNumber = JdDailyFault.objects.filter(jdDaiFauID=row.id).count()
            #JdDailyInspection.objects.create(jdDaiInsFaultNumber=row.jdDaiInsFaultNumber)
            info['counts'] = row.jdDaiInsFaultNumber
            info['temperature'] = row.jdDaiInsTemperature
            info['weather'] = row.jdDaiInsWeather
            info['checkdate'] = row.jdDaiInsTime
            info['checkperson'] = row.jdDaiInsFaultPeople
            info['department'] = row.jdDaiInsFaultCompany
            dailyInfo.append(info)
        data = {
            "status": 1,
            "message": u"检查记录页面数据显示成功",
            "data": dailyInfo
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"检查记录页面数据显示失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电日巡巡查纪录修改数据
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_MOD(request):
    if request.GET:
        a = JdDailyInspection.objects.get(id=request.GET['check_id'])
        # b = Company.objects.filter(cname=a.jdDaiInsFaultCompany)
        b = Company.objects.all()
        deptInfo = []
        for bs in b:
            info={}
            info['organize_id'] = bs.corganize_id
            info['name'] = bs.cname
            deptInfo.append(info)
        aas = Person.objects.all()
        userInfo = []
        for aa in aas:
            info = {}
            info['user_id'] = aa.puserid
            info['real_name'] = aa.puser.last_name
            userInfo.append(info)
        weatherInfo = [{"weather_id": 1, "weather": u"晴"},
                       {"weather_id": 2, "weather": u"多云"},
                       {"weather_id": 3, "weather": u"小雨"},
                       {"weather_id": 4, "weather": u"大雨"},
                       {"weather_id": 5, "weather": u"下雪"},
                       {"weather_id": 6, "weather": u"大风"}]
        recordInfo = JdDailyInspection.objects.get(id=request.GET['check_id'])
        return render(request,'JDXJ/RCXC/JDXJ_RCXC_XCJL_MOD.html', {'userInfo': userInfo, 'deptInfo': deptInfo,
                                                                    'weatherInfo': weatherInfo, 'recordInfo': recordInfo})

# 功能：机电日巡巡查纪录修改数据保存
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_MODSAVE(request):
    if request.POST:
        row = JdDailyInspection.objects.get(id=request.POST['check_id'])
        row.jdDaiInsFaultCompanyid = request.POST['organize_id']
        row.jdDaiInsFaultCompany = request.POST['name']
        row.jdDaiInsFaultPeopleid = request.POST['user_id']
        row.jdDaiInsFaultPeople = request.POST['real_name']
        row.jdDaiInsTime = request.POST['checkdate']
        row.jdDaiInsWeather = request.POST['weather']
        row.jdDaiInsTemperature = request.POST['temperature']
        row.save()
        data = {
            "status": 1,
            "message": u"检查记录页面修改数据保存成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"检查记录页面修改数据保存失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电日巡巡查纪录删除页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_DEL(request):
    if request.GET:
        bb = request.GET['check_id']
        cc = JdDailyFault.objects.filter(jdDaiFauID=bb)
        if not cc.exists():
            JdDailyInspection.objects.get(id=bb).delete()
            data = {
                "status": 1,
                "message": u"检查记录页面删除成功",
                "data": []
            }
            return JsonResponse(data)
        else:
            data = {
                "status": 0,
                "message": u"检查记录页面删除失败",
                "data": []
            }
            return JsonResponse(data)
    else:
        pass

# 功能：机电日巡巡查纪录查看页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE(request):
    return render(request, 'JDXJ/RCXC/JDXJ_RCXC_XCJL_SEE.html',{'check_id':request.GET['check_id']})

# 功能：机电日巡巡查纪录查看新增页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE_ADD(request):
    if request.GET:
        locationInfo = JdDeviceLoc.objects.all()
        # sysInfo = JdSystem.objects.all()
        return render(request,'JDXJ/RCXC/JDXJ_RCXC_XCJL_SEE_ADD.html', {'check_id': request.GET['check_id'],
                                                                        'locationInfo': locationInfo})
# 功能：机电日巡巡查纪录查看选完位置选系统
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE_ADD_LOCSYS(request):
    if request.GET:
        locInfo = JdDeviceDet.objects.filter(jdDeviceLocation=request.GET['location_id'])
        locIn = []
        aa = []
        for loc in locInfo:
            info = {}
            info['id'] = loc.jdSystemName.id
            info['name'] = loc.jdSystemName.systemName
            if loc.jdSystemName.id not in aa:
                locIn.append(info)
                aa.append(loc.jdSystemName.id)
        data = {
            "status": 1,
            "message": u"检查记录页面成功",
            "data": locIn
        }
    return JsonResponse(data)
# 功能：机电日巡巡查纪录页面查看新增页面之后选择系统再选择设备类别
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE_ADD_SYS(request):
    if request.GET:
        rows = JdDeviceDet.objects.filter(jdSystemName=request.GET['sys_id'], jdDeviceLocation=request.GET['location_id'])
        sysInfo= []
        aa = []
        for row in rows:
            info = {}
            info['devicetype_id'] = row.jdDeviceCategory.id
            info['devicetype_name'] = row.jdDeviceCategory.deviceName
            if row.jdDeviceCategory.deviceName not in aa:
                sysInfo.append(info)
                aa.append(row.jdDeviceCategory.deviceName)
        data = {
            "status": 1,
            "message": u"检查记录页面成功",
            "data": sysInfo
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"检查记录页面失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电日巡巡查纪录页面查看新增页面之后选择设备类别之后返回设备信息和检查内容信息
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE_ADD_DEVICETYPE(request):
    if request.GET:
        rows = JdDeviceDet.objects.filter(jdDeviceLocation=request.GET['location_id'],
                                          jdDeviceCategory=request.GET['devicetype_id'])
        deviceInfo = []
        for row in rows:
            info1={}
            info1['device_id'] = row.id
            info1['device_name'] = row.jdDeviceName
            deviceInfo.append(info1)
        if len(rows) == 0:
            data = {
                "status": 0,
                "message": u"请添加下端设备",
                "data": []
            }
            return JsonResponse(data)
        c = JdCheck.objects.get(jdCheckDevCat=request.GET['devicetype_id'],
                                jdCheckType=1)
        bbs = JdInspection.objects.filter(jdInspectionProjectId=c.id)
        try:
            if len(bbs) == 0:
                data = {
                    "status": 0,
                    "message": u"请在该设备下添加检测项目",
                    "data": []
                }
            return JsonResponse(data)
        except:
            JCPROInfo = []
            for bb in bbs:
                info={}
                info['pro_id'] = bb.id
                info['pro_name'] = bb.jdInspectionProject
                JCPROInfo.append(info)
            data = {
                "status": 1,
                "message": u"选择设备后返回信息成功",
                "data": {"devicelist": deviceInfo, "JCPROInfo": JCPROInfo}
            }
            return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"选择设备后返回信息失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：选择检查项目之后返回检查内容
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE_ADD_Checkitem(request):
    if request.GET:
        row = JdInspection.objects.get(id=request.GET['jcpro_id'])
        try:
            itemInfo = []
            info ={}
            info['content_id'] = row.id
            info['content_name'] = row.jdInspectionContent
            itemInfo.append(info)
            data = {
                "status": 1,
                "message": u"选择检测内容信息成功",
                "data": itemInfo
            }
            return JsonResponse(data)
        except:
            data = {
                "status": 0,
                "message": u"请添加检查内容",
                "data": itemInfo
            }
            return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"选择检测内容信息失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电日巡巡查纪录页面查看新增后保存
# 传入参数：request
# 返回参数：responese
def JDXJ_RCXC_XCJL_SEE_ADDSAVE(request):
    if request.POST:
        a = 'ED' + str(time.strftime("%Y%m%d")) + '100'
        print 'a',a
        try:
            JdDailyFault.objects.get(jdDaiFauTaskNum=a)
            aa = JdDailyFault.objects.latest('jdDaiFauTaskNum')
            dd = aa.jdDaiFauTaskNum
            c = dd[10:13]
            h = int(c) + int(1)
            d = time.strftime("%Y%m%d")
            TaskNum = 'ED' + str(d) + str(h)
        except:
            TaskNum = a
        b = JdInspection.objects.get(id=request.POST['content_id'])

        AAA = DailyFaultHistoryList.objects.create(daiFauHisListPeople=request.user.last_name,
                                                   daiFauHisListTime=time.strftime("%Y%m%d  %H:%M:%S"),
                                                   daiFauHisListRemrk=request.POST['remark'],
                                                   daiFauHisListProject=JdInspection.objects.get(id=request.POST['pro_id']).jdInspectionProject,
                                                   daiFauHisListDeviceName=JdDeviceDet.objects.get(id=request.POST['device_id']).jdDeviceName,
                                                   daiFauHisListLeader=u'李部长',
                                                   daiFauHisListStatue=u'故障录入')
        FFF = DailyFaultHistoryList.objects.create(daiFauHisListPeople=u'王二',
                                                   daiFauHisListTime=time.strftime("%Y%m%d  %H:%M:%S"),
                                                   daiFauHisListRemrk=request.POST['remark'],
                                                   daiFauHisListProject=JdInspection.objects.get(id=request.POST['pro_id']).jdInspectionProject,
                                                   daiFauHisListDeviceName=JdDeviceDet.objects.get(id=request.POST['device_id']).jdDeviceName,
                                                   daiFauHisListLeader=u'张总',
                                                   daiFauHisListStatue=u'故障追踪')
        BBB = JdDailyFault.objects.create(jdDaiFauLocation=JdDeviceLoc.objects.get(id=request.POST['location_id']),
                                          jdDaiFauSystem=JdSystem.objects.get(id=request.POST['sys_id']),
                                          jdDaiFauCategory=JdDeviceCol.objects.get(id=request.POST['devicetype_id']),
                                          jdDaiFauDeviceName=JdDeviceDet.objects.get(id=request.POST['device_id']),
                                          jdDaiFauProject=JdInspection.objects.get(id=request.POST['pro_id']),
                                          jdDaiFauCon=b.jdInspectionContent,
                                          jdDaiFauRemark=request.POST['remark'],
                                          jdDaiFauID=JdDailyInspection.objects.get(id=request.POST['check_id']),
                                          jdDaiFauTaskNum=TaskNum)
        BBB.jdDaiFauHistory.add(AAA,FFF)
        photos = request.POST['photoList']
        json_photos = json.loads(str(photos))
        for json_photo in json_photos:
            add_photo = DailyFaultHistoryPhoto.objects.create(daiFauPhoto=json_photo['file_url'])
            dd_photo = JdDailyFaultPhoto.objects.create(daiFauPhoto=json_photo['file_url'])
            AAA.Photos.add(add_photo)
            BBB.jdDaiFauPhoto.add(dd_photo)
        data = {
            "status": 1,
            "message": u"查看新增后保存信息成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"查看新增后保存信息失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电日巡巡查纪录页面查看页面数据加载
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE_DATA(request):
    if request.GET:
        rows = JdDailyFault.objects.filter(jdDaiFauID=request.GET['check_id'])
        itemInfo = []
        for row in rows:
            info = {}
            info['record_id'] = row.id
            info['record_code'] = row.jdDaiFauTaskNum
            info['sys_name'] = row.jdDaiFauSystem.systemName
            info['device_name'] = row.jdDaiFauDeviceName.jdDeviceName
            info['check_pro'] = row.jdDaiFauProject.jdInspectionProject
            info['check_content'] = row.jdDaiFauCon
            info['remark'] = row.jdDaiFauRemark
            ds = row.jdDaiFauPhoto.all()
            if len(ds) > 0:
                info['photo_count'] = 1
            else:
                info['photo_count'] = 0
            itemInfo.append(info)
        data = {
            "status": 1,
            "message": u"查看页面数据加载成功",
            "data": itemInfo
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"查看页面数据加载失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电日巡巡查纪录页面查看修改页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE_MOD(request):
    if request.GET:
        locationInfo = JdDeviceLoc.objects.all()
        sysInfo = JdSystem.objects.all()
        FaultInfo = JdDailyFault.objects.get(jdDaiFauID=request.GET['check_id'],
                                             id=request.GET['record_id'])
        jdpro = JdInspection.objects.filter(jdInspectionProjectId=FaultInfo.jdDaiFauProject.jdInspectionProjectId)
        return render(request, 'JDXJ/RCXC/JDXJ_RCXC_XCJL_SEE_MOD.html', {
                                                                         'check_id': request.GET['check_id'],
                                                                         'record_id': request.GET['record_id'],
                                                                         'locationInfo': locationInfo,
                                                                         'sysInfo': sysInfo,
                                                                         'FaultInfo': FaultInfo,
                                                                         'jdpro':jdpro})

# 功能：机电日巡巡查纪录页面查看修改后保存
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE_MODSAVE(request):
    if request.POST:
        rows = JdDailyFault.objects.get(id=request.POST['record_id'])
        rows.jdDaiFauLocation = JdDeviceLoc.objects.get(id=request.POST['location_id'])
        rows.jdDaiFauSystem = JdSystem.objects.get(id=request.POST['sys_id'])
        rows.jdDaiFauCategory = JdDeviceCol.objects.get(id=request.POST['devicetype_id'])
        rows.jdDaiFauDeviceName = JdDeviceDet.objects.get(id=request.POST['device_id'])
        rows.jdDaiFauProject = JdInspection.objects.get(id=request.POST['pro_id'])
        rows.jdDaiFauCon = request.POST['content_id']
        rows.jdDaiFauRemark = request.POST['remark']
        rows.jdDaiFauID = JdDailyInspection.objects.get(id=request.POST['check_id'])
        photos = request.POST['photoList']
        json_photos = json.loads(str(photos))
        for json_photo in json_photos:
            dd_photo = JdDailyFaultPhoto.objects.create(daiFauPhoto=json_photo['file_url'])
            rows.jdDaiFauPhoto.add(dd_photo)
        rows.save()
        data = {
            "status": 1,
            "message": u"修改后保存成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"修改后保存失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电日巡巡查纪录页面查看页面删除
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE_DEL(request):
    if request.GET:
        JdDailyFault.objects.get(id=request.GET['record_id']).delete()
        data = {
            "status": 1,
            "message": u"查看页面删除成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"查看页面删除失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：机电日巡巡查纪录页面查看页面媒体信息
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_XCJL_SEE_MEDIA(request):
    if request.GET:
        row = JdDailyFault.objects.get(id=request.GET['record_id'])
        ds = row.jdDaiFauPhoto.all()
        mediaInfo = []
        for d in ds:
            info={}
            url = d.daiFauPhoto
            info['file_url'] = url
            mediaInfo.append(info)
        data = {
            "status": 1,
            "message": u"查看页面媒体信息成功",
            "data": mediaInfo
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"查看页面媒体信息失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：日常维护纪录页面新增
# 传入参数：request
# 返回参数：responese
#@login_required()
# def JDXJ_RCXC_WHJL_ADD(request):
#     if 1==1:
#         userInfo = [{"user_id": 1, "real_name": u"李华"}, {"user_id": 2, "real_name": u"张三"}]
#         rows = JdDailyFault.objects.all()
#         sysInfo = []
#         locationInfo = []
#         for row in rows:
#             a = row.jdDaiFauLocation.id
#             b = row.jdDaiFauSystem.id
#             locationInfo.append(JdDeviceLoc.objects.get(id=a))
#             sysInfo.append(JdSystem.objects.get(id=b))
#         return render(request, 'JDXJ/RCXC/JDXJ_RCXC_WHJL_ADD.html', {'userInfo': userInfo, 'user_id': 1,'locationInfo': locationInfo, 'sysInfo': sysInfo})

# 功能：日常维护纪录页面新增保存
# 传入参数：request
# 返回参数：responese
#@login_required()
# def JDXJ_RCXC_WHJL_ADDSAVE(request):
#     if request.POST:
#         rows = JdDailyFault.objects.all()
#         for row in rows:
#             a = row.id
#             print a
#         b = JdInspection.objects.get(id=request.POST['pro_id'])
#         JdDailyMaintain.objects.create(jdDaiMaiLocation=JdDeviceLoc.objects.get(id=request.POST['location_id']),
#                                        jdDaiMaiSystem=JdSystem.objects.get(id=request.POST['sys_id']),
#                                        jdDaiMaiCategory=JdDeviceCol.objects.get(id=request.POST['devicetype_id']),
#                                        jdDaiMaiDeviceName=JdDeviceDet.objects.get(id=request.POST['device_id']),
#                                        jdDaiMaiProject=request.POST['pro_id'],
#                                        jdDaiMaiCon=request.POST['content_id'],
#                                        jdDaiMaiRemark=request.POST['remark'],
#                                        jdDaiMaiPhoto=request.POST['photoList'],
#                                        jdDaiMaiTime=request.POST['checkdate'],
#                                        jdDaiMaiID=request.POST['check_id'])
#         data = {
#             "status": 1,
#             "message": u"日常维护纪录页面新增保存成功",
#             "data": []
#         }
#         return JsonResponse(data)
#     else:
#         data = {
#             "status": 0,
#             "message": u"日常维护纪录页面新增保存失败",
#             "data": []
#         }
#         return JsonResponse(data)

# 功能：日常维护纪录页面数据加载
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_RCXC_WHJL_DATA(request):
    if request.GET:
        rows = JdDailyMaintain.objects.all()
        maintainInfo = []
        for row in rows:
            info ={}
            info['record_id'] = row.id
            info['record_code'] = row.jdDaiMaiTaskNum
            info['sys_name'] = row.jdDaiMaiSystem.systemName
            info['device_name'] = row.jdDaiMaiDeviceName.jdDeviceName
            info['check_pro'] = row.jdDaiMaiProject.jdInspectionProject
            info['check_content'] = row.jdDaiMaiCon
            info['remark'] = row.jdDaiMaiRemark
            info['done_date'] = row.jdDaiMaiTime
            maintainInfo.append(info)
        data = {
            "status": 1,
            "message": u"日常维护纪录页面数据加载成功",
            "data": maintainInfo
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"日常维护纪录页面数据加载失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：日巡、经检、定检内容页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def  JDXJ_RCXC_WHJL_SEE_MEDIA(request):
    if request.GET:
        row = JdDailyMaintain.objects.get(id=request.GET['record_id'])
        ds = row.jdDaiMaiPhoto.all()
        mediaInfo = []
        for d in ds:
            info = {}
            url = d.daiMaiPhoto
            info['file_url'] = url
            mediaInfo.append(info)
        data = {
            "status": 1,
            "message": u"查看页面媒体信息成功",
            "data": mediaInfo
        }
        return JsonResponse(data)

# 功能：日巡、经检、定检内容页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ(request):
    return render(request, 'JDXJ/RCXC/JDXJ_JJDJ.html',{'tp':request.GET['tp']})

# 功能：新增检查记录页面
# 传入参数：request
# 返回参数：responese
def JDXJ_JJDJ_ADD(request):
    tp = request.GET['tp']
    bbs = Company.objects.all()
    deptInfo = []
    b = []
    for bb in bbs:
        info = {}
        info['organize_id'] = bb.corganize_id
        info['name'] = bb.cname
        if bb.cname not in b:
            b.append(bb.cname)
            deptInfo.append(info)
    aas = Person.objects.all()
    userInfo = []
    for aa in aas:
        info = {}
        info['user_id'] = aa.puserid
        info['real_name'] = aa.puser.last_name
        userInfo.append(info)
    loginInfo = []
    dd = User.objects.get(last_name=request.user.last_name)
    cc = Person.objects.get(puser_id=dd.id)
    info['user_id'] = cc.puserid
    info['real_name'] = cc.puser.last_name
    info['organize_id'] = cc.pdept_id
    loginInfo.append(info)
    return render(request,'JDXJ/RCXC/JDXJ_JJDJ_ADD.html',{'tp':tp, 'userInfo':userInfo,'deptInfo':deptInfo,'loginInfo':loginInfo})

# 功能：新增检查记录页面保存
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_ADDSAVE(request):
    if request.POST:
        JdOftenInspection.objects.create(jdOftInsType=request.POST['tp'],
                                         jdOftInsTimeSta=request.POST['start_time'],
                                         jdOftInsTimeEnd=request.POST['end_time'],
                                         jdOftInsPeopleid=request.POST['user_id'],
                                         jdOftInsPeople=request.POST['real_name'],
                                         jdOftInsCompanyid=request.POST['organize_id'],
                                         jdOftInsCompany=request.POST['name'])
        company = []
        info = {}
        info['organize_id'] = request.POST['organize_id']
        info['user_id'] = request.POST['user_id']
        company.append(info)
        data = {
            "status": 1,
            "message": u"新增检查记录页面保存成功",
            "data": company
        }
        return JsonResponse(data)
    else:
        data = {
        "status": 0,
        "message": u"新增检查记录页面保存失败",
        "data": []
    }
    return JsonResponse(data)

# 功能：经检、定检内容表格数据获取
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_DATA (request):
    if request.GET:
        jjdjInfo = []
        rows = JdOftenInspection.objects.filter(jdOftInsType=request.GET['tp'])
        for row in rows:
            info={}
            info['check_id'] = row.id
            info['person'] = row.jdOftInsPeople
            info['department'] = row.jdOftInsCompany
            info['start_time'] = row.jdOftInsTimeSta
            info['end_time'] = row.jdOftInsTimeEnd
            info['status'] = row.jdOftInsStatus
            jjdjInfo.append(info)
        data = {
            "status": 1,
            "message": u"定检内容表格数据获取成功",
            "data": jjdjInfo
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"定检内容表格数据获取失败",
            "data": []
        }
    return JsonResponse(data)

# 功能：修改检查记录页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_MOD (request):
    if request.GET:
        recordInfo = JdOftenInspection.objects.get(jdOftInsType=request.GET['tp'], id=request.GET['check_id'])
        # a = JdDailyInspection.objects.get(id=request.GET['check_id'])
        # b = Company.objects.filter(cname=a.jdDaiInsFaultCompany)
        b = Company.objects.all()
        deptInfo = []
        for bs in b:
            info = {}
            info['organize_id'] = bs.corganize_id
            info['name'] = bs.cname
            deptInfo.append(info)
        aas = Person.objects.all()
        userInfo = []
        for aa in aas:
            info = {}
            info['user_id'] = aa.puserid
            info['real_name'] = aa.puser.last_name
            userInfo.append(info)
        return render(request, 'JDXJ/RCXC/JDXJ_JJDJ_MOD.html', {'tp': request.GET['tp'],
                                                                'check_id': request.GET['check_id'],
                                                                'recordInfo': recordInfo,
                                                                'deptInfo': deptInfo,
                                                                'userInfo': userInfo})

# 功能：新增检查记录页面保存
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_MODSAVE (request):
    if request.POST:
        row = JdOftenInspection.objects.get(jdOftInsType=request.POST['tp'], id=request.POST['check_id'])
        row.jdOftInsTimeSta = request.POST['start_time']
        row.jdOftInsTimeEnd = request.POST['end_time']
        row.jdOftInsType = request.POST['tp']
        row.jdOftInsCompany = request.POST['name']
        row.jdOftInsCompanyid = request.POST['organize_id']
        row.jdOftInsPeople = request.POST['real_name']
        row.jdOftInsPeopleid = request.POST['user_id']
        row.save()
        data = {
            "status": 1,
            "message": u"新增检查记录页面保存成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"新增检查记录页面保存失败",
            "data": []
        }
    return JsonResponse(data)

# 功能：检查记录页面删除
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_DEL (request):
    if request.GET:
        aa = request.GET['tp']
        bb = request.GET['check_id']
        cc = JdOftenFault.objects.filter(jdOftFauID=bb)
        if not cc.exists():
            JdOftenInspection.objects.get(jdOftInsType=aa, id=bb).delete()
        data = {
            "status": 1,
            "message": u"检查记录页面删除成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"检查记录页面删除失败",
            "data": []
        }
    return JsonResponse(data)

# 功能：检查记录故障详情页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_SEE (request):
    if request.GET:
        return render(request, 'JDXJ/RCXC/JDXJ_JJDJ_SEE.html', {'tp':request.GET['tp'],
                                                                'check_id':request.GET['check_id']})

# 功能：故障详情新增页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_SEE_ADD (request):
    if request.GET:
        locationInfo = JdDeviceLoc.objects.all()
        # sysInfo = JdSystem.objects.all()
        return render(request, 'JDXJ/RCXC/JDXJ_JJDJ_SEE_ADD.html', {'locationInfo':locationInfo,
                                                                    # 'sysInfo':sysInfo,
                                                                    'tp':request.GET['tp'],
                                                                    'check_id':request.GET['check_id']})

# 功能：经检定检页面查看新增页面之后选择设备类别之后返回设备信息和检查内容信息
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_SEE_ADD_DEVICETYPE (request):
    if request.GET:
        rows = JdDeviceDet.objects.filter(jdDeviceLocation=request.GET['location_id'],
                                          jdDeviceCategory=request.GET['devicetype_id'])
        d = request.GET['tp']
        h = int(d)+1
        c = JdCheck.objects.get(jdCheckDevCat=request.GET['devicetype_id'],
                                jdCheckType=h)
        d = request.GET['tp']
        bbs = JdInspection.objects.filter(jdInspectionProjectId=c.id)
        deviceInfo = []
        JCPROInfo = []
        for row in rows:
            info = {}
            info['device_id'] = row.id
            info['device_name'] = row.jdDeviceName
            deviceInfo.append(info)
        try:
            if len(bbs) == 0:
                data = {
                    "status": 0,
                    "message": u"请在该设备下添加检测项目",
                    "data": []
                }
            return JsonResponse(data)
        except:
            for bb in bbs:
                info = {}
                info['pro_id'] = bb.id
                info['pro_name'] = bb.jdInspectionProject
                JCPROInfo.append(info)
            data = {
                "status": 1,
                "message": u"选择设备后返回信息成功",
                "data": {"devicelist": deviceInfo, "JCPROInfo": JCPROInfo}
            }
    else:
        data = {
            "status": 0,
            "message": u"选择设备后返回信息失败",
            "data": []
        }
    return JsonResponse(data)

# 功能：机电日巡巡查纪录页面查看新增后保存
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_SEE_ADDSAVE (request):
    if request.POST:
        if int(request.POST['tp'])==1:
            a = 'EO'+str(time.strftime("%Y%m%d"))+'100'
            try:
                JdOftenFault.objects.filter(jdOftFauTaskNum=a)
                aa = JdOftenFault.objects.latest('jdOftFauTaskNum')
                dd = aa.jdOftFauTaskNum
                c = dd[10:13]
                h = int(c) + int(1)
                d = time.strftime("%Y%m%d")
                TaskNum = 'EO' + str(d) + str(h)
            except:
                TaskNum = a
        else:
            a = 'ER' + str(time.strftime("%Y%m%d")) + '100'
            try:
                JdOftenFault.objects.get(jdOftFauTaskNum=a)
                aa = JdOftenFault.objects.latest('jdOftFauTaskNum')
                dd = aa.jdOftFauTaskNum
                c = dd[10:13]
                h = int(c) + int(1)
                d = time.strftime("%Y%m%d")
                TaskNum = 'ER' + str(d) + str(h)
            except JdOftenFault.DoesNotExist:
                TaskNum = a
        AAA = OftenFaultHistoryList.objects.create(OftFauHisListPeople=request.user.last_name,
                                                   OftFauHisListTime=time.strftime("%Y%m%d  %H:%M:%S"),
                                                   OftFauHisListRemrk=request.POST['remark'],
                                                   OftFauHisListLeader=u'张大大',
                                                   OftFauHisListStatue=u'故障录入',
                                                   OftFauHisListProject=JdInspection.objects.get(id=request.POST['pro_id']).jdInspectionProject,
                                                   OftFauHisListDeviceName=JdDeviceDet.objects.get(id=request.POST['device_id']).jdDeviceName)
        b = JdInspection.objects.get(id=request.POST['content_id'])
        BBB = JdOftenFault.objects.create(jdOftFauLocation=JdDeviceLoc.objects.get(id=request.POST['location_id']),
                                          jdOftFauSystem=JdSystem.objects.get(id=request.POST['sys_id']),
                                          jdOftFauCategory=JdDeviceCol.objects.get(id=request.POST['devicetype_id']),
                                          jdOftFauDeviceName=JdDeviceDet.objects.get(id=request.POST['device_id']),
                                          jdOftFauProject=JdInspection.objects.get(id=request.POST['pro_id']),
                                          jdOftFauCon=b.jdInspectionContent,
                                          jdOftFauRemark=request.POST['remark'],
                                          jdOftFauID=JdOftenInspection.objects.get(id=request.POST['check_id']),
                                          jdOftFauTaskNum=TaskNum)
        BBB.jdOftFauHistory.add(AAA)
        photos = request.POST['photoList']
        json_photos = json.loads(str(photos))
        for json_photo in json_photos:
            add_photo = OftenFaultHistoryPhoto.objects.create(oftFauPhoto=json_photo['file_url'])
            dd_photo = JdOftFauPhoto.objects.create(oftFauPhoto=json_photo['file_url'])
            AAA.Photos.add(add_photo)
            BBB.jdOftFauPhoto.add(dd_photo)
        data = {
            "status": 1,
            "message": u"机电日巡巡查纪录页面查看新增后保存成功",
            "data": []
        }
    else:
        data = {
            "status": 0,
            "message": u"机电日巡巡查纪录页面查看新增后保存失败",
            "data": []
        }
    return JsonResponse(data)

# 功能：查看页面数据加载
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_SEE_DATA (request):
    if request.GET:
        c = JdOftenInspection.objects.get(id=request.GET['check_id'], jdOftInsType=request.GET['tp'])
        rows = JdOftenFault.objects.filter(jdOftFauID=c.id)
        itemInfo = []
        for row in rows:
            info = {}
            info['record_id'] = row.id
            info['record_code'] = row.jdOftFauTaskNum
            info['sys_name'] = row.jdOftFauSystem.systemName
            info['device_name'] = row.jdOftFauDeviceName.jdDeviceName
            info['check_pro'] = row.jdOftFauProject.jdInspectionProject
            info['check_content'] = row.jdOftFauCon
            info['remark'] = row.jdOftFauRemark
            itemInfo.append(info)
        data = {
            "status": 1,
            "message": u"查看页面数据加载成功",
            "data": itemInfo
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"查看页面数据加载失败",
            "data": []
        }
        return JsonResponse(data)

# 功能：故障详情修改页面
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_SEE_MOD (request):
    if request.GET:
        c = JdOftenInspection.objects.get(id=request.GET['check_id'], jdOftInsType=request.GET['tp'])
        FaultInfo = JdOftenFault.objects.get(jdOftFauID=c.id, id=request.GET['record_id'])
        jdoftpro = JdInspection.objects.filter(jdInspectionProjectId=FaultInfo.jdOftFauProject.jdInspectionProjectId)

        locationInfo = JdDeviceLoc.objects.all()
        sysInfo = JdSystem.objects.all()
        return render(request, 'JDXJ/RCXC/JDXJ_JJDJ_SEE_MOD.html', {'tp': request.GET['tp'],
                                                                    'check_id': request.GET['check_id'],
                                                                    'locationInfo': locationInfo,
                                                                    'sysInfo': sysInfo,
                                                                    'FaultInfo': FaultInfo,
                                                                    'jdpro':jdoftpro})

# 功能：机电定检、经检故障详情修改保存
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_SEE_MODSAVE (request):
    if request.POST:
        rows = JdOftenFault.objects.get(id=request.POST['record_id'])
        rows.jdOftFauLocation = JdDeviceLoc.objects.get(id=request.POST['location_id'])
        rows.jdOftFauSystem = JdSystem.objects.get(id=request.POST['sys_id'])
        rows.jdOftFauCategory = JdDeviceCol.objects.get(id=request.POST['devicetype_id'])
        rows.jdOftFauDeviceName = JdDeviceDet.objects.get(id=request.POST['device_id'])
        rows.jdOftFauProject = JdInspection.objects.get(id=request.POST['pro_id'])
        rows.jdOftFauCon = request.POST['content_id']
        rows.jdOftFauRemark = request.POST['remark']
        rows.jdOftFauID = JdOftenInspection.objects.get(id=request.POST['check_id'])
        photos = request.POST['photoList']
        json_photos = json.loads(str(photos))
        for json_photo in json_photos:
            dd_photo = JdOftFauPhoto.objects.create(oftFauPhoto=json_photo['file_url'])
            rows.jdOftFauPhoto.add(dd_photo)
        rows.save()
        data = {
            "status": 1,
            "message": u"故障详情修改保存成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"故障详情修改保存失败",
            "data": []
        }
    return JsonResponse(data)

# 功能：机电定检、经检故障详情删除
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_SEE_DEL (request):
    if request.GET:
        JdOftenFault.objects.get(id=request.GET['record_id']).delete()
        data = {
            "status": 1,
            "message": u"故障详情修改删除成功",
            "data": []
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"故障详情修改删除失败",
            "data": []
        }
    return JsonResponse(data)

# 功能：经检定检页面查看页面媒体信息
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_JJDJ_SEE_MEDIA (request):
    if request.GET:
        row = JdOftenFault.objects.get(id=request.GET['record_id'])
        ds = row.jdOftFauPhoto.all()
        mediaInfo = []
        for d in ds:
            info = {}
            url = d.oftFauPhoto
            info['file_url'] = url
            mediaInfo.append(info)
        data = {
            "status": 1,
            "message": u"查看页面媒体信息成功",
            "data": mediaInfo
        }
        return JsonResponse(data)
    else:
        data = {
            "status": 0,
            "message": u"查看页面媒体信息失败",
            "data": []
        }
    return JsonResponse(data)

# 功能：机电故障信息页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_GZXX_index (request):
    return render(request, 'JDXJ/GZXX/JDXJ_GZXX_index.html')

# 功能：机电故障信息加载
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_GZXX_DATA (request):
    if int(request.GET['check_type'])==1:
        rows = JdDailyFault.objects.all()
        dailyInfo =[]
        for row in rows:
            info={}
            info['checked_id'] = row.id
            info['sys_name'] = row.jdDaiFauSystem.systemName
            info['task_number'] = row.jdDaiFauTaskNum
            info['equipment_name'] = row.jdDaiFauDeviceName.jdDeviceName
            info['check_item'] = row.jdDaiFauProject.jdInspectionProject
            info['check_content'] = row.jdDaiFauCon
            info['check_remark'] = row.jdDaiFauRemark
            c = row.jdDaiFauTaskNum
            info['datetime'] = c[2:10]
            dailyInfo.append(info)
        data = {
            "status": 1,
            "message": u"查看页面媒体信息成功",
            "data": dailyInfo
        }
        return JsonResponse(data)
    else:
        a = request.GET['check_type']
        b = int(a) -1
        aas = JdOftenInspection.objects.filter(jdOftInsType=b)
        for aa in aas:
            bbs = JdOftenFault.objects.filter(jdOftFauID=aa.id)
            oftenInfo =[]
            for bb in bbs:
                info = {}
                info['checked_id'] = bb.id
                info['sys_name'] = bb.jdOftFauSystem.systemName
                info['task_number'] = bb.jdOftFauTaskNum
                info['equipment_name'] = bb.jdOftFauDeviceName.jdDeviceName
                info['check_item'] = bb.jdOftFauProject.jdInspectionProject
                info['check_content'] = bb.jdOftFauCon
                info['check_remark'] = bb.jdOftFauRemark
                c = bb.jdOftFauTaskNum
                info['datetime'] = c[2:10]
                oftenInfo.append(info)
            data = {
                "status": 1,
                "message": u"查看页面媒体信息成功",
                "data": oftenInfo
            }
            return JsonResponse(data)

# 功能：机电故障信息详情
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_GZXX_XQ (request):
    if int(request.GET['check_type']) == 1:
        n = JdDailyFault.objects.filter(id=request.GET['checked_id'])
        for ns in n:
            rows = ns.jdDaiFauHistory.all()
            historyList=[]
            for row in rows:
                info={}
                info['device_name'] = row.daiFauHisListDeviceName
                info['user_name'] = row.daiFauHisListPeople
                info['task_date'] = row.daiFauHisListTime
                info['status_name'] = row.daiFauHisListStatue
                if row.daiFauHisListRemrk!=None:
                    info['type'] = 1
                    info['name'] = row.daiFauHisListProject
                    info['remark'] = row.daiFauHisListRemrk
                    krows = row.Photos.all()
                    photo = []
                    for krow in krows:
                        url = krow.daiFauPhoto
                        info1 ={}
                        info1['url'] = url
                        photo.append(info1)
                    info['urls'] = photo
                historyList.append(info)
    if int(request.GET['check_type']) == 2 or int(request.GET['check_type']) == 3:
        bb = request.GET['check_type']
        dd = int(bb) - 1
        aas = JdOftenInspection.objects.filter(jdOftInsType=dd)
        for aa in aas:
            n = JdOftenFault.objects.filter(jdOftFauID=aa.id,id=request.GET['checked_id'])
            for ns in n:
                rows = ns.jdOftFauHistory.all()
                oftenInfo = []
                for row in rows:
                    historyList = []
                    for row in rows:
                        info = {}
                        info['device_name'] = row.OftFauHisListDeviceName
                        info['user_name'] = row.OftFauHisListPeople
                        info['task_date'] = row.OftFauHisListTime
                        info['status_name'] = row.OftFauHisListStatue
                        if row.OftFauHisListStatue!=None:
                            info['type'] = 1
                            info['name'] = row.OftFauHisListProject
                            info['remark'] = row.OftFauHisListRemrk
                            krows = row.Photos.all()
                            photo = []
                            for krow in krows:
                                url = krow.oftFauPhoto
                                info1={}
                                info1['url'] = url
                                photo.append(info1)
                        info['urls'] = photo
                    historyList.append(info)
    return render(request, 'JDXJ/GZXX/JDXJ_GZXX_XQ.html', {'historyList': historyList})


# 功能：机电故障统计详情
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_GZTJ_index (request):
    return render(request, 'JDXJ/GZTJ/JDXJ_GZTJ_index.html')

# 功能：机电故障统计按系统分
# 传入参数：request
# 返回参数：responese
def JDXJ_GZTJ_SYS (request):
    return render(request, 'JDXJ/GZTJ/JDXJ_GZTJ_SYS.html')

# 功能：按系统统计故障信息
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_GZTJ_SYS_DATA (request):
    sta = request.POST['start_time']
    end = request.POST['end_time']
    if int(request.POST['mold'])==1:
        rows = JdDailyInspection.objects.all()
        dailysysInfo =[]
        aas = JdSystem.objects.all()
        for aa in aas:
            info = {}
            total = 0
            for row in rows:
                a = row.jdDaiInsTime
                if sta<=a<=end:
                    hhs = JdDailyInspection.objects.filter(jdDaiInsTime=a)
                    for hh in hhs:
                        c = JdDailyFault.objects.filter(jdDaiFauID=hh.id, jdDaiFauSystem=aa.id).count()
                        total = total+c
            if total == 0:
                pass
            else:
                info['count'] = total
                info['name'] = aa.systemName
                dailysysInfo.append(info)
        data = {
            "status": 1,
            "message": u"日常检查查看系统故障分类信息成功",
            "data": dailysysInfo
        }
        return JsonResponse(data)
    if int(request.POST['mold'])==2 or int(request.POST['mold'])==3:
        bb = request.POST['mold']
        dd = int(bb)-1
        rows = JdOftenInspection.objects.filter(jdOftInsType=dd)
        oftenInfo = []
        aas = JdSystem.objects.all()
        for aa in aas:
            info = {}
            total = 0
            for row in rows:
                a = row.jdOftInsTimeSta
                b = row.jdOftInsTimeEnd
                if sta <=a and b<= end:
                    hhs = JdOftenInspection.objects.filter(jdOftInsTimeSta=a, jdOftInsTimeEnd=b)
                    for hh in hhs:
                        c = JdOftenFault.objects.filter(jdOftFauID=hh.id, jdOftFauSystem=aa.id).count()
                        total = total + c
            if total == 0:
                pass
            else:
                info['count'] = total
                info['name'] = aa.systemName
                oftenInfo.append(info)
        data = {
            "status": 1,
            "message": u"查看系统故障分类信息成功",
            "data": oftenInfo
        }
        return JsonResponse(data)
    if int(request.POST['mold']) == 4:
        oftens = JdOftenInspection.objects.all()
        dailys = JdDailyInspection.objects.all()
        totalInfo = []
        aas = JdSystem.objects.all()
        for aa in aas:
            info = {}
            dailytotal = 0
            oftentoal = 0
            for daily in dailys:
                a = daily.jdDaiInsTime
                if sta <= a <= end:
                    hhs = JdDailyInspection.objects.filter(jdDaiInsTime=a)
                    for hh in hhs:
                        c = JdDailyFault.objects.filter(jdDaiFauID=hh.id, jdDaiFauSystem=aa.id).count()
                        dailytotal = dailytotal + c
            for often in oftens:
                a = often.jdOftInsTimeSta
                b = often.jdOftInsTimeEnd
                if sta <=a and b<= end:
                    hhs = JdOftenInspection.objects.filter(jdOftInsTimeSta=a, jdOftInsTimeEnd=b)
                    for hh in hhs:
                        c = JdOftenFault.objects.filter(jdOftFauID=hh.id, jdOftFauSystem=aa.id).count()
                        oftentoal = oftentoal + c
            total = dailytotal+oftentoal
            if total == 0:
                pass
            else:
                info['count'] = total
                info['name'] = aa.systemName
                totalInfo.append(info)
        data = {
            "status": 1,
            "message": u"系统分类故障汇总查询信息成功",
            "data": totalInfo
        }
        return JsonResponse(data)

# 功能：按设备类型统计页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_GZTJ_SB (request):
    return render(request, 'JDXJ/GZTJ/JDXJ_GZTJ_SB.html')

# 功能：按设备类别统计故障信息
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_GZTJ_SB_DATA (request):
    sta = request.POST['start_time']
    end = request.POST['end_time']
    if int(request.POST['mold'])==1:
        rows = JdDailyInspection.objects.all()
        dailysysInfo =[]
        aas = JdDeviceCol.objects.all()
        for aa in aas:
            info = {}
            total = 0
            for row in rows:
                a = row.jdDaiInsTime
                if sta<=a<=end:
                    hhs = JdDailyInspection.objects.filter(jdDaiInsTime=a)
                    for hh in hhs:
                        c = JdDailyFault.objects.filter(jdDaiFauID=hh.id, jdDaiFauCategory=aa.id).count()
                        total = total+c
            if total == 0:
                pass
            else:
                info['count'] = total
                info['name'] = aa.deviceName
                dailysysInfo.append(info)
        data = {
            "status": 1,
            "message": u"日常检查查看设备类别故障分类信息成功",
            "data": dailysysInfo
        }
        return JsonResponse(data)
    if int(request.POST['mold'])==2 or int(request.POST['mold'])==3:
        bb = request.POST['mold']
        dd = int(bb)-1
        rows = JdOftenInspection.objects.filter(jdOftInsType=dd)
        oftenInfo = []
        aas = JdDeviceCol.objects.all()
        for aa in aas:
            info = {}
            total = 0
            for row in rows:
                a = row.jdOftInsTimeSta
                b = row.jdOftInsTimeEnd
                if sta <=a and b<= end:
                    hhs = JdOftenInspection.objects.filter(jdOftInsTimeSta=a, jdOftInsTimeEnd=b)
                    for hh in hhs:
                        c = JdOftenFault.objects.filter(jdOftFauID=hh.id, jdOftFauCategory=aa.id).count()
                        total = total + c
            if total == 0:
                pass
            else:
                info['count'] = total
                info['name'] = aa.deviceName
                oftenInfo.append(info)
        data = {
            "status": 1,
            "message": u"查看设备故障类别分类信息成功",
            "data": oftenInfo
        }
        return JsonResponse(data)
    if int(request.POST['mold']) == 4:
        oftens = JdOftenInspection.objects.all()
        dailys = JdDailyInspection.objects.all()
        totalInfo = []
        aas = JdDeviceCol.objects.all()
        for aa in aas:
            info = {}
            dailytotal = 0
            oftentoal = 0
            for daily in dailys:
                a = daily.jdDaiInsTime
                if sta <= a <= end:
                    hhs = JdDailyInspection.objects.filter(jdDaiInsTime=a)
                    for hh in hhs:
                        c = JdDailyFault.objects.filter(jdDaiFauID=hh.id, jdDaiFauCategory=aa.id).count()
                        dailytotal = dailytotal + c
            for often in oftens:
                a = often.jdOftInsTimeSta
                b = often.jdOftInsTimeEnd
                if sta <=a and b<= end:
                    hhs = JdOftenInspection.objects.filter(jdOftInsTimeSta=a, jdOftInsTimeEnd=b)
                    for hh in hhs:
                        c = JdOftenFault.objects.filter(jdOftFauID=hh.id, jdOftFauCategory=aa.id).count()
                        oftentoal = oftentoal + c
            total = dailytotal+oftentoal
            if total == 0:
                pass
            else:
                info['count'] = total
                info['name'] = aa.deviceName
                totalInfo.append(info)
        data = {
            "status": 1,
            "message": u"设备类别故障分类汇总查询信息成功",
            "data": totalInfo
        }
        return JsonResponse(data)

# 功能：按单个设备统计页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_GZTJ_ONE (request):
    return render(request, 'JDXJ/GZTJ/JDXJ_GZTJ_ONE.html')

# 功能：按单个设备统计页面跳转
# 传入参数：request
# 返回参数：responese
@login_required()
def JDXJ_GZTJ_ONE_DATA (request):
    sta = request.POST['start_time']
    end = request.POST['end_time']
    if int(request.POST['mold']) == 1:
        rows = JdDailyInspection.objects.all()
        dailysysInfo = []
        aas = JdDeviceDet.objects.all()
        for aa in aas:
            info = {}
            total = 0
            for row in rows:
                a = row.jdDaiInsTime
                if sta <= a <= end:
                    hhs = JdDailyInspection.objects.filter(jdDaiInsTime=a)
                    for hh in hhs:
                        c = JdDailyFault.objects.filter(jdDaiFauID=hh.id, jdDaiFauDeviceName=aa.id).count()
                        total = total + c
            if total == 0:
                pass
            else:
                info['count'] = total
                info['name'] = aa.jdDeviceName
                dailysysInfo.append(info)
        data = {
            "status": 1,
            "message": u"日常检查查看单个设备故障信息成功",
            "data": dailysysInfo
        }
        return JsonResponse(data)
    if int(request.POST['mold']) == 2 or int(request.POST['mold']) == 3:
        bb = request.POST['mold']
        dd = int(bb) - 1
        rows = JdOftenInspection.objects.filter(jdOftInsType=dd)
        oftenInfo = []
        aas = JdDeviceDet.objects.all()
        for aa in aas:
            info = {}
            total = 0
            for row in rows:
                a = row.jdOftInsTimeSta
                b = row.jdOftInsTimeEnd
                if sta <= a and b <= end:
                    hhs = JdOftenInspection.objects.filter(jdOftInsTimeSta=a, jdOftInsTimeEnd=b)
                    for hh in hhs:
                        c = JdOftenFault.objects.filter(jdOftFauID=hh.id, jdOftFauDeviceName=aa.id).count()
                        total = total + c
            if total == 0:
                pass
            else:
                info['count'] = total
                info['name'] = aa.jdDeviceName
                oftenInfo.append(info)
        data = {
            "status": 1,
            "message": u"查看单个设备故障查询信息成功",
            "data": oftenInfo
        }
        return JsonResponse(data)
    if int(request.POST['mold']) == 4:
        oftens = JdOftenInspection.objects.all()
        dailys = JdDailyInspection.objects.all()
        totalInfo = []
        aas = JdDeviceDet.objects.all()
        for aa in aas:
            info = {}
            dailytotal = 0
            oftentoal = 0
            for daily in dailys:
                a = daily.jdDaiInsTime
                if sta <= a <= end:
                    hhs = JdDailyInspection.objects.filter(jdDaiInsTime=a)
                    for hh in hhs:
                        c = JdDailyFault.objects.filter(jdDaiFauID=hh.id, jdDaiFauDeviceName=aa.id).count()
                        dailytotal = dailytotal + c
            for often in oftens:
                a = often.jdOftInsTimeSta
                b = often.jdOftInsTimeEnd
                if sta <= a and b <= end:
                    hhs = JdOftenInspection.objects.filter(jdOftInsTimeSta=a, jdOftInsTimeEnd=b)
                    for hh in hhs:
                        c = JdOftenFault.objects.filter(jdOftFauID=hh.id, jdOftFauDeviceName=aa.id).count()
                        oftentoal = oftentoal + c
            total = dailytotal + oftentoal
            if total == 0:
                pass
            else:
                info['count'] = total
                info['name'] = aa.jdDeviceName
                totalInfo.append(info)
        data = {
            "status": 1,
            "message": u"单个设备故障汇总查询信息成功",
            "data": totalInfo
        }
        return JsonResponse(data)
